package com.dhcc.cdc.busi.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.cdc.busi.entity.CdcBsEmrStructEntity;
import com.dhcc.cdc.busi.entity.CdcEmrEntity;
import com.dhcc.cdc.busi.entity.CdcExaminateEntity;
import com.dhcc.cdc.util.DHCAI;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.cdc.busi.dao.CdcBsExamStructMapper;
import com.dhcc.cdc.busi.service.ICdcBsExamStructService;
import com.dhcc.cdc.busi.entity.CdcBsExamStructEntity;

/**
 * 表数据服务层接口
 * 
 * @ClassName: CdcBsExamStructServiceImpl
 * @Description: TODO
 * @author sunyj
 * @date 2023-12-24 13:18:33
 */
@Service("cdcBsExamStructService")
public class CdcBsExamStructServiceImpl extends BaseServiceImpl<CdcBsExamStructMapper, CdcBsExamStructEntity> implements ICdcBsExamStructService {

	@Override
	public List<CdcBsExamStructEntity> page(Page<CdcBsExamStructEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdcBsExamStructEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public void examStructByNLP(String dhcAiUrl, List<CdcExaminateEntity> examRecordList) {
		//获取电子病历结构化信息
		JSONArray emrArray = new JSONArray();
		//Map<String, Object> emrMap = new HashMap<String, Object>();
		// 开启多线程查询科室
		List<Future> list = new ArrayList<Future>();
		ExecutorService examPool = Executors.newFixedThreadPool(5);
		long nowTime = new Date().getTime();
		for(CdcExaminateEntity exam : examRecordList){

			//查询该检查是否已被解析存储 并且更新日期为前天
			int count = this.selectCount(new EntityWrapper<CdcBsExamStructEntity>().eq("exam_id", exam.getId()));

			Date updateDate = exam.getRepDate();
			//如果报告日期为空 默认为2天前
			if(updateDate==null){
				Calendar canlendar = Calendar.getInstance(); // java.util包
				canlendar.add(Calendar.DATE, -2); // 日期减 如果不够减会将月变动
				updateDate = canlendar.getTime();
			}
			long updateTime = updateDate.getTime();
			long isTime = nowTime - updateTime;
			if(count==0||isTime<=(24*60*60*1000)){//未被解析或者报告日期小于一天
				String examDesc = exam.getExamDesc();
				String examResult = exam.getResultDesc();
				Long examId = exam.getId();
				String examDescNlpStr = "";
				if(!CommonUtil.isEmpty(examDesc)){
					CdcBsExamStructServiceImpl.DHCAICallable c = new CdcBsExamStructServiceImpl.DHCAICallable(dhcAiUrl,exam.getDescription(),examDesc,exam.getRepDate());
					Future f = examPool.submit(c);
					list.add(f);
					try {
						JSONArray obj = (JSONArray) f.get();
						examDescNlpStr = obj.toJSONString();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
				}
				String examResultNlpStr = "";
				if(!CommonUtil.isEmpty(examResult)){
					CdcBsExamStructServiceImpl.DHCAICallable c = new CdcBsExamStructServiceImpl.DHCAICallable(dhcAiUrl,exam.getDescription(),examResult,exam.getRepDate());
					Future f = examPool.submit(c);
					list.add(f);
					try {
						JSONArray obj = (JSONArray) f.get();
						examResultNlpStr = obj.toJSONString();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
				}
				if(count!=0){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Wrapper wpr = new EntityWrapper<CdcBsExamStructEntity>().eq("exam_id", examId);
					this.updateForSet("emr_nlp_desc='"+examDescNlpStr+"',emr_nlp_result='"+examResultNlpStr+"'", wpr);
				}else{
					CdcBsExamStructEntity examStructEntity = new CdcBsExamStructEntity();
					examStructEntity.setExamId(examId);
					examStructEntity.setExamNlpDesc(examDescNlpStr);
					examStructEntity.setExamNlpResult(examResultNlpStr);
					examStructEntity.setExamDate(exam.getRepDate());
					examStructEntity.setExamUpdateDate(exam.getUpdateDate());
					examStructEntity.setEpisodeId(exam.getEpisodeId());
					examStructEntity.setExamUpdateDate(new Date());
					this.insert(examStructEntity);
				}
			}
		}
		examPool.shutdown();
	}

	/**
	 *
	 * @ClassName: DHCAICallable
	 * @Description ：   调用东华语义分析多线程
	 * @Date: 2019年8月1日 下午2:34:08
	 * @Author WuJiaFeng
	 * @Version 1.0 DHC-VTE
	 */
	private class DHCAICallable implements Callable<Object> {
		private String title;
		private String content;
		private String dhcai_url;
		private Date date;

		public DHCAICallable(String dhcai_url,String title,String content,Date date) {
			this.dhcai_url = dhcai_url;
			this.title = title;
			this.content = content;
			this.date = date;
		}

		@Override
		public Object call() throws Exception {
			JSONArray resultArr = DHCAI.parse(dhcai_url,content);
			return resultArr;
		}
	}

}